const axios = require('axios');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const getData = (res) => new Promise(resolve => {
    const chunks = []
    res.data.on('data', chunk => {
        chunks.push(chunk)
    })
    res.data.on('end', () => {
        const buffer = Buffer.concat(chunks)
        const str = iconv.decode(buffer, 'gbk')
        resolve(str)
    })
})
const getNewsData = async () => {
  try{
    const res = await axios.get('https://house.hexun.com/news/',{
      responseType:"stream"
    });
    const data = await getData(res);
    const $ = cheerio.load(data);
    const parent = $('.sxypb ul');
    let datas = [];
    $(parent).each((i,el) => {
      $(el).children().each((i,lis)=>{
        const tagA = $(lis).find('a');
        datas.push({
          question:tagA.text(),
          id:tagA.attr('href'),
          url:tagA.attr('href')
        });
      })
    });
    return datas
  } catch(error) {
    console.log(error);
    return {
      code: 400,
      ...error
    } 
  }
}

module.exports = getNewsData;